﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

using CE.Database;
using CE.Global;
using CE.HR;
using CE.Job;
using CE.User;

public partial class HR_user_details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.HRId] == null)
        {
            Response.Redirect("login.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.Params["uid"] != null)
            {
                Int64 _uid = Int64.Parse(Request.Params["uid"].ToString());

                Account _account = new Account();
                AccountDB _accountdb = new AccountDB();
                _account = _accountdb.GetAccount(_uid);

                Profile _profile = new Profile();
                ProfileDB _profiledb = new ProfileDB();
                _profile = _profiledb.GetProfile(_uid);

                lblName.Text = _profile.Salutation + " " + _profile.Name;
                lblContactNumber.Text = _profile.ContactNumber;
                lblDOB.Text = _profile.DOB.ToString("dd MMM yyyy");
                lblEmailAddress.Text = _account.Email;
                lblQualification.Text = _profile.Qualification;
                lblYearsExperience.Text = _profile.YearsExperience;
                lblSalesExperience.Text = _profile.SalesExperience;
                lblEmpStatus.Text = _profile.EmploymentStatus;
                lblOccupation.Text = _profile.Occupation;
                lblCountry.Text = _profile.PreferredCountry;
                lblCreatedDate.Text = _profile.CreatedDate.ToString();

                if (_profile.AttachCV != "")
                {
                    if (System.IO.File.Exists(_profile.AttachCV))
                    {
                        hidCV.Value = _profile.AttachCV;
                    }
                    else
                    {
                        btnDownloadCV.Text = "File not exists";
                        btnDownloadCV.Enabled = false;
                    }
                }
                else
                {
                    btnDownloadCV.Text = "No CV upload";
                    btnDownloadCV.Enabled = false;
                }

                if (_profile.VoiceRecord != "")
                {
                    if (System.IO.File.Exists(_profile.VoiceRecord))
                    {
                        hidVR.Value = _profile.VoiceRecord;
                    }
                    else
                    {
                        btnDownloadVR.Text = "File not exists";
                        btnDownloadVR.Enabled = false;
                    }
                }
                else
                {
                    btnDownloadVR.Text = "No Record";
                    btnDownloadVR.Enabled = false;
                }

                GetResultTable();
            }
        }
    }
    private void GetResultTable()
    {
        HRAccountDB _accountdb = new HRAccountDB();
        HRAccount _account = _accountdb.GetAccount(Int64.Parse(Session[CE.Global.General.HRId].ToString()));

        MyDatabase _db = new MyDatabase();
        DataSet _dsResult = new DataSet();
        string _spName = "[sprocApplicationSelectListByUserID]";
        SortedList _spara = new SortedList();
        _spara.Add("@UID", Int64.Parse(Request.Params["uid"].ToString()));
        _dsResult = _db.GetDataSet(_spName, _spara);

        PagedDataSource pageds = new PagedDataSource();
        pageds.DataSource = _dsResult.Tables[0].DefaultView;
        pageds.AllowPaging = true;
        pageds.PageSize = 30;

        int curpage = 0;

        if (Request.QueryString["page"] != null)
        {
            curpage = Convert.ToInt32(Request.QueryString["page"]);
        }
        else
        {
            curpage = 1;
        }

        pageds.CurrentPageIndex = curpage - 1;
        if (pageds.Count > 0)
            lblCurrpage.Text = "";
        if (curpage == 1 && pageds.DataSourceCount > pageds.PageSize)
        {
            lblCurrpage.Text = "Pages: 1";
            lblCurrpageTop.Text = "Pages: 1";
        }
        else if (pageds.DataSourceCount == 0)
        {
            lblCurrpage.Text = "No data to display.";
            lblCurrpageTop.Text = "";
        }
        else if (curpage > 1 && pageds.DataSourceCount > pageds.PageSize)
        {
            lblCurrpage.Text = "Pages: <a href='user-details.aspx?uid=" + Request.Params["uid"].ToString() + "&page=1'>1</a>";
            lblCurrpageTop.Text = "Pages: <a href='job-post.aspx?uid=" + Request.Params["uid"].ToString() + "&page=1'>1</a>";
        }
        for (int i = 2; i <= pageds.PageCount; i++)
        {
            if (i == curpage)
            {
                lblCurrpage.Text = lblCurrpage.Text + " " + i.ToString();
                lblCurrpageTop.Text = lblCurrpageTop.Text + " " + i.ToString();
            }
            else
            {
                lblCurrpage.Text = lblCurrpage.Text + " <a href='job-post.aspx?page=" + i.ToString() + "&uid=" + Request.Params["uid"].ToString() + "'>" + i.ToString() + "</a>";
                lblCurrpageTop.Text = lblCurrpageTop.Text + " <a href='job-post.aspx?page=" + i.ToString() + "&uid=" + Request.Params["uid"].ToString() + "'>" + i.ToString() + "</a>";
            }
        }

        gvUser.DataSource = pageds;
        gvUser.DataBind();
    }
    protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblRemark = (Label)e.Row.FindControl("lblRemark");
            Label lblVerifiedBy = (Label)e.Row.FindControl("lblVerifiedBy");
            Label lblRefUserID = (Label)e.Row.FindControl("lblRefUserID");

            if (lblRemark.Text == "")
                lblRemark.Text = "-";

            Int64 _hrid = Int64.Parse(lblVerifiedBy.Text);
            HRAccountDB _accountdb = new HRAccountDB();
            HRAccount _account = _accountdb.GetAccount(_hrid);

            if (_account != null)
                lblVerifiedBy.Text = _account.Name;
            else
                lblVerifiedBy.Text = "-";

            Int64 _uid = Int64.Parse(lblRefUserID.Text);
            Profile _profile = new Profile();
            ProfileDB _profiledb = new ProfileDB();
            _profile = _profiledb.GetProfile(_uid);

            if (_profile != null)
                lblRefUserID.Text = _profile.Name;
        }
    }
    protected void btnDownloadCV_Click(object sender, EventArgs e)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(hidCV.Value);
        if (!file.Exists)
        {
            Response.StatusCode = 500;
            Response.End();
            return;
        }
        Response.Clear();
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.MinValue);
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(file.FullName);
        ApplicationInstance.CompleteRequest();
        Response.End();
    }
    protected void btnDownloadVR_Click(object sender, EventArgs e)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(hidVR.Value);
        if (!file.Exists)
        {
            Response.StatusCode = 500;
            Response.End();
            return;
        }
        Response.Clear();
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.MinValue);
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(file.FullName);
        ApplicationInstance.CompleteRequest();
        Response.End();
    }
}
